package com.elitecrm.service.coresync;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class CoreSyncServiceServlet extends HttpServlet{
	private static final Log log = LogFactory.getLog(CoreSyncServiceServlet.class);
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String respText = "0";
		String relationId = req.getParameter("relation_id");
		log.debug("Receive request with relation_id "+relationId);
		String dataSourceId = req.getParameter("project_id");
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		CoreSyncService css = (CoreSyncService)ac.getBean("syncCoreService");
		DataSource dataSource = (DataSource)ac.getBean(dataSourceId);
		try {
			css.syncCoreSystemByRelationId(relationId,dataSource);
			respText = "1";
		} catch (Exception e) {
			log.error("",e);
		}
		resp.getOutputStream().print(respText);
	}

}
